const moment = require('moment');
const BufferReader = require('./bufferreader');
exports.buffer2Date = (buf) => {
  const reader = new BufferReader(buf),
    year = reader.read(2).readUInt16LE(),
    month = reader.read(1).readUInt8(),
    date = reader.read(1).readUInt8(),
    hour = reader.read(1).readUInt8(),
    minute = reader.read(1).readUInt8(),
    second = reader.read(1).readUInt8();
    console.log(`${year}-${month}-${date} ${hour}:${minute}:${second}`)
  return moment(`${year}-${month}-${date} ${hour}:${minute}:${second}`, 'yyyy-MM-dd HH:mm:ss');
}

exports.date2Buffer = (dateStr) => {
  const date = moment(dateStr), buf = Buffer.alloc(8);
  buf.writeUInt16LE(date.year(), 0);
  buf.writeUInt8(date.month() + 1, 2);
  buf.writeUInt8(date.date(), 3);
  buf.writeUInt8(date.hour(), 4);
  buf.writeUInt8(date.minute(), 5);
  buf.writeUInt8(date.second(), 6);
  buf.writeUInt8(0xFF, 7);
  return buf;
}
